﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DTO;
using BUS;
using Utility;

public partial class Admin_DanhSachSanPham : System.Web.UI.Page
{
    protected string CurrentColumnSort
    {
        get
        {
            return ViewState["CurrentColumnSort"].ToString();
        }
        set
        {
            ViewState["CurrentColumnSort"] = value;
        }
    }
    protected int STT
    {
        get
        {
            return int.Parse(ViewState["STT"].ToString());
        }
        set
        {
            ViewState["STT"] = value;
        }
    }
    protected string OrderType
    {
        get
        {
            return ViewState["OrderType"].ToString();
        }
        set
        {
            ViewState["OrderType"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            this.CurrentColumnSort = SanPhamColumn.MaSanPham.ToString();
            this.OrderType = OrderDirection.ASC.ToString();
            LoadDropDownList();
            LoadData();
        }
    }
    private void LoadDropDownList()
    {

        LoaiCollection loaicollection = LoaiBUS.GETALL();
        ddl_tenloaisanpham.DataSource = loaicollection;
        ddl_tenloaisanpham.DataTextField = "TenLoai";
        ddl_tenloaisanpham.DataValueField = "MaLoai";
        ddl_tenloaisanpham.DataBind();

        ddl_tenloaisanpham.Items.Insert(0, new ListItem("All", "0"));

        ddl_tenloaisanpham.SelectedValue = "0";


        NhaSanXuatCollection nsxcollection = NhaSanXuatBUS.GETALL();
        ddl_nhasanxuat.DataSource = nsxcollection;
        ddl_nhasanxuat.DataTextField = "TenNhaSanXuat";
        ddl_nhasanxuat.DataValueField = "MaNhaSanXuat";
        ddl_nhasanxuat.DataBind();

        ddl_nhasanxuat.Items.Insert(0, new ListItem("All", "0"));
        ddl_nhasanxuat.SelectedValue = "0";
    }

    private void LoadData()
    {
        try
        {
            STT = (pager.CurrentIndex - 1) * pager.PageSize + 1;
            int total = 0;
            Criteria cri = new Criteria();
            
            if (txt_tensanpham.Text != " ")
                cri.add(Condition.AND, SanPhamColumn.TenSanPham.ToString(), Condition.LIKE, txt_tensanpham.Text);

            if (ddl_tenloaisanpham.SelectedValue != "0")
                cri.add(Condition.AND, SanPhamColumn.MaLoai.ToString(), Condition.EQUAL, int.Parse(ddl_tenloaisanpham.SelectedValue));

            if (ddl_nhasanxuat.SelectedValue != "0")
                cri.add(Condition.AND, SanPhamColumn.MaNhaSanXuat.ToString(), Condition.EQUAL, int.Parse(ddl_nhasanxuat.SelectedValue));
            pager.PageSize = 5;// int.Parse(ConfigurationManager.AppSettings["pagesize"]);

            SanPhamCollection sanphamcollection = SanPhamBUS.GET(cri.Criter, pager.CurrentIndex, pager.PageSize, this.CurrentColumnSort, this.OrderType, out total);
            rpt_SanPham.DataSource = sanphamcollection;
            rpt_SanPham.DataBind();
            pager.ItemCount = total;

            if (pager.ItemCount <= 0 || pager.ItemCount <= pager.PageSize)
                pager.Visible = false;
            else
                pager.Visible = true;
        }
        catch
        {

        }
    }


    protected void btn_timkiem_Click(object sender, EventArgs e)
    {
        LoadData();
    }

    protected void btn_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < rpt_SanPham.Items.Count; i++)
        {
            CheckBox cb_Delete = (CheckBox)rpt_SanPham.Items[i].FindControl("cb_Delete");
            if (cb_Delete.Checked == true)
            {
                HiddenField hf_ID = (HiddenField)rpt_SanPham.Items[i].FindControl("hf_ID");
                int id = int.Parse(hf_ID.Value);
                SanPhamBUS.DELETE(id);
            }
        }
        LoadData();
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void rpt_SanPham_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            SanPham sp = (SanPham)e.Item.DataItem;
            Label lb_STT = (Label)e.Item.FindControl("lb_STT");
            lb_STT.Text = Convert.ToString(STT + e.Item.ItemIndex);
            Label lb_tenloai = (Label)e.Item.FindControl("lb_Tenloai");

            Literal lit_Gia = (Literal)e.Item.FindControl("lit_Gia");
            lit_Gia.Text = String.Format("{0:0,0 VNĐ}", sp.Gia);

            Criteria cri = new Criteria();
            cri.add(Condition.AND, LoaiColumn.MaLoai, Condition.EQUAL, sp.MaLoai);
            int total = 0;
            LoaiCollection loaicollection = LoaiBUS.GET(cri.Criter, 0, 0, LoaiColumn.MaLoai.ToString(), OrderDirection.ASC.ToString(), out total);
            if (loaicollection.Count > 0)
            {
                lb_tenloai.Text = loaicollection[0].TenLoai;
            }
            Label lb_nhasx = (Label)e.Item.FindControl("lb_nhasx");
            Criteria cri1 = new Criteria();
            int total1 = 0;
            cri1.add(Condition.AND, NhaSanXuatColumn.MaNhaSanXuat, Condition.EQUAL, sp.MaNhaSanXuat);
            NhaSanXuatCollection NSXcollection = NhaSanXuatBUS.GET(cri1.Criter, 0, 0, NhaSanXuatColumn.MaNhaSanXuat.ToString(), OrderDirection.ASC.ToString(), out total1);
            if (loaicollection.Count > 0)
            {
                lb_nhasx.Text = NSXcollection[0].TenNhaSanXuat;
            }
            HiddenField hf_ID = (HiddenField)e.Item.FindControl("hf_ID");
            hf_ID.Value = sp.MaSanPham.ToString();
            //HyperLink hl_tensp = (HyperLink)e.Item.FindControl("hl_tensp");
            //hl_tensp.Text = sp.TenSanPham;
            //hl_tensp.NavigateUrl = "~/Admin/CapNhatSanPham.aspx?id=" + sp.MaSanPham.ToString();
            HyperLink hl_Update = (HyperLink)e.Item.FindControl("hl_Update");
            hl_Update.NavigateUrl = "~/Admin/CapNhatSanPham.aspx?id=" + sp.MaSanPham.ToString();
        }

    }


    protected void rpt_SanPham_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (this.OrderType == OrderDirection.ASC.ToString())
            this.OrderType = OrderDirection.DESC.ToString();
        else
            this.OrderType = OrderDirection.ASC.ToString();


        switch (e.CommandArgument.ToString())
        {
            case "TenSP":
                this.CurrentColumnSort =
                    SanPhamColumn.TenSanPham.ToString();
                break;
        }
        LoadData();
    }

}